import { NextFunction, Request, Response, } from "express";
import MovieService from "../services/MovieService";
export class MovieControll {
  /**
  * @description: 新增接口
  * @author: jiangqi 2024-03-13 21:34:40
  * @param: Movie
  */
  static async add(req: Request, res, next: NextFunction) {
    try {
      const result = await MovieService.add(req.body)
      if (!Array.isArray(result)) {
        res.sendRes({
          code: 200,
          data: result,
          success: true,
          message: '添加成功'
        })
      } else {
        res.sendRes({
          code: 200,
          data: null,
          success: false,
          message: result[0]
        })
      }
    } catch (error) {
      next(error)
    }
  }

  /**
  * @description: 删除接口
  * @author: jiangqi 2024-03-13 21:35:06
  * @param: id
  */
  static async del(req: Request, res, next: NextFunction) {
    const { id } = req.params
    try {
      await MovieService.delete(id)
      res.sendRes({
        code: 200,
        data: null,
        success: true,
        message: '删除成功'
      })
    } catch (error) {
      next(error)
    }
  }

  /**
  * @description: 删除name包含某个字符的所有数据
  * @author: jiangqi 2024-03-17 22:21:59
  * @param: name
  */
  static async delName(req: Request, res, next: NextFunction) {
    const { name } = req.body
    try {
      const result = await MovieService.deleteMany(name)
      res.sendRes({
        code: 200,
        data: null,
        success: true,
        message: '删除成功'
      })
    } catch (error) {
      next(error)
    }
  }

  /**
  * @description: 更新接口
  * @author: jiangqi 2024-03-13 21:45:33
  * @param: IMovie
  */
  static async update(req: Request, res, next: NextFunction) {
    try {
      const result = await MovieService.update(req.body)
      if (result.length <= 0) {
        res.sendRes({
          code: 200,
          data: null,
          success: true,
          message: '修改成功'
        })
      } else {
        res.sendErr({
          code: 200,
          message: result[0]
        })
      }
    } catch (error) {
      next(error)
    }
  }

  /**
  * @description: id查询
  * @author: jiangqi 2024-03-13 23:12:45
  * @param: id
  */
  static async find(req: Request, res, next: NextFunction) {
    const { id } = req.params
    try {
      const result = await MovieService.findById(id)
      res.sendRes({
        data: result
      })
    } catch (error) {
      next(error)
    }
  }

  /**
  * @description: 模糊查询
  * @author: jiangqi 2024-03-13 23:12:45
  * @param: id
  */
  static async query(req: Request, res, next: NextFunction) {
    try {
      const result = await MovieService.find(req.query)
      if (Array.isArray(result) && result.length > 0) {
        res.sendErr({
          code: 400,
          message: result
        })
      } else {
        res.sendRes({
          data: result
        })
      }
    } catch (error) {
      next(error)
    }
  }
}